<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @on-load="onLoad"
    >
      <template slot="menuLeft" v-if="systemType === 0">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          plain
          v-if="permission.dicttype_add"
          @click.stop="handleAdd"
          >添 加
        </el-button>
        <!--
        <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          v-if="permission.dicttype_delete"
          @click="handleDelete"
          >删 除
        </el-button>
      -->
      </template>
      <template slot-scope="scope" slot="menu" v-if="systemType === 0">
        <div class="table-btn-group">
          <el-button
            type="text"
            icon="el-icon-plus"
            size="mini"
            v-if="scope.row.parentId == 0"
            @click.stop="handleAddSon(scope.row, scope.index)"
            >添加字典子项
          </el-button>
          <el-button
            type="text"
            icon="el-icon-edit"
            size="mini"
            @click.stop="handleEdit(scope.row, scope.index)"
            >编辑
          </el-button>
          <el-button
            type="text"
            icon="el-icon-delete"
            size="mini"
            @click.stop="handleDel(scope.row, scope.index)"
            >删除
          </el-button>
        </div>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getList, getDetail, add, update, remove } from "@/api/system/dictitem";
import { mapGetters } from "vuex";

export default {
  name: "dict-item",
  props: {
    dictType: String,
    systemType: Number,
  },
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      option: {
        height: "auto",
        calcHeight: 210,
        size: "medium",
        align: "center",
        menuAlign: "center",
        menuWidth: 300,
        indexLabel: "序号",
        searchShow: false,
        searchMenuSpan: 6,
        tip: false,
        page: null,
        tree: true,
        border: true,
        index: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        viewBtn: false,
        selection: false,
        column: [
          {
            label: "类型",
            prop: "type",
            rules: [
              {
                required: true,
                message: "请输入类型",
                trigger: "blur",
              },
            ],
          },
          {
            label: "父主键",
            prop: "parentId",
            type: "number",
            hide: true,
            addDisplay: false,
            editDisplay: false,
            rules: [
              {
                required: true,
                message: "请输入父主键",
                trigger: "blur",
              },
            ],
          },
          {
            label: "数据值",
            prop: "value",
            type: "number",
            rules: [
              {
                required: true,
                message: "请输入数据值",
                trigger: "blur",
              },
            ],
          },
          {
            label: "标签名",
            prop: "label",
            rules: [
              {
                required: true,
                message: "请输入标签名",
                trigger: "blur",
              },
            ],
          },
          {
            label: "描述",
            prop: "description",
            rules: [
              {
                required: true,
                message: "请输入描述",
                trigger: "blur",
              },
            ],
          },
          {
            label: "排序",
            prop: "sort",
            type: "number",
            rules: [
              {
                required: true,
                message: "请输入排序",
                trigger: "blur",
              },
            ],
          },
          {
            label: "备注信息",
            prop: "remark",
            rules: [
              {
                required: false,
                message: "请输入备注信息",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.dicttype_add, false),
        viewBtn: this.vaildData(this.permission.dicttype_view, false),
        delBtn: this.vaildData(this.permission.dicttype_delete, false),
        editBtn: this.vaildData(this.permission.dicttype_edit, false),
      };
    },
  },
  methods: {
    handleAdd() {
      this.$refs.crud.option.column.filter((item) => {
        if (item.prop === "type") {
          item.value = this.dictType;
          item.addDisabled = true;
        }
        if (item.prop === "parentId") {
          item.value = 0;
          item.addDisabled = true;
        }
      });
      this.$refs.crud.rowAdd();
    },
    handleAddSon(row) {
      this.$refs.crud.option.column.filter((item) => {
        if (item.prop === "type") {
          item.value = this.dictType;
          item.addDisabled = true;
        }
        if (item.prop === "parentId") {
          item.value = row.id;
          item.editDisabled = true;
        }
      });
      this.$refs.crud.rowAdd();
    },
    handleEdit(row, index) {
      this.$refs.crud.option.column.filter((item) => {
        if (item.prop === "type") {
          item.value = row.type;
          item.editDisabled = true;
        }
      });
      this.$refs.crud.rowEdit(row, index);
    },
    handleDel(row) {
      this.rowDel(row);
    },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          done();
          this.onLoad();
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          done();
          this.onLoad();
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad();
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    onLoad() {
      // console.log({ type: this.dictType });
      this.loading = true;
      getList({ type: this.dictType }).then((res) => {
        this.data = res.data.data;
        this.loading = false;
      });
    },
  },
};
</script>

<style>
</style>
